package grapid.cipher;

import java.security.MessageDigest;

/**
 * MD5加密类
 * 
 * @author 来源于网络
 * @version 1.0
 * 
 */
public class MD5 {

	/**
	 * 加密
	 * 
	 * @param s
	 *            待加密字符串
	 * @return 加密后字符串
	 */
	public static final String encode(String s) {
		char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexdigits[byte0 >>> 4 & 0xf];
				str[k++] = hexdigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			System.out.println("MD5加密时出错：" + e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

}
